package servlets;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Connectivity.ConnectionManager;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {


	/**
	 * 
	 */
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
				           throws ServletException, java.io.IOException {

		
	try
	{	    
		String uname=null;
		String upassword=null;
		int flag=0;
		HttpSession session=null;
		String username="",emp_id="",role_id="";
		
		
	    uname=(request.getParameter("uname"));
	    upassword=(request.getParameter("upassword"));
	    Connection con=ConnectionManager.getConnection();
	  	PreparedStatement prst=con.prepareStatement
	 	("select (name||' '||coalesce(father_name,' ')||' '||last_name)as name,emp_id,role_id from employee_master where loginid='"+uname+"' AND password='"+upassword+"'");   
	    ResultSet rs=prst.executeQuery();
	         
	       while(rs.next()) 
	     {
	  	   
	  	    username = rs.getString(1);
	  	    emp_id= rs.getString(2);
	  	    role_id = rs.getString(3); 
	  	 
	  	    flag=1;
	     }
	     if(flag==1)
	     {   session = request.getSession(true);
	    	 session.setAttribute("uname",username);
	  	   session.setAttribute("emp_id",emp_id);
	  	   session.setAttribute("role_id",role_id);
	  	 response.sendRedirect("/Genius/form/general/userLogged.jsp"); //logged-in page
	     }
	     else if(uname.equals(null) || upassword.equals(null))
	     {response.sendRedirect("/Genius/form/general/Login.jsp");}
	     else
	     {// session = request.getSession(false);
	    	// session.setAttribute("uname",username);
		  	//   session.setAttribute("emp_id",emp_id);
		  	 //  session.setAttribute("role_id",role_id);
	     response.sendRedirect("/Genius/form/general/invalidLogin.jsp");
	     }
	    	     
	} 
			
			
	catch (Throwable theException) 	    
	{
	     System.out.println(theException); 
	}
	       }


public void doPost(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, java.io.IOException {

	
	try
	{	    
		String uname=null;
		String upassword=null;
		int flag=0;
		HttpSession session=null;
		String username="",emp_id="",role_id="";
		
		
	    uname=(request.getParameter("uname"));
	    upassword=(request.getParameter("upassword"));
	    Connection con=ConnectionManager.getConnection();
	  	PreparedStatement prst=con.prepareStatement
	 	("select (name||' '||coalesce(father_name,' ')||' '||last_name)as name,emp_id,role_id from employee_master where loginid='"+uname+"' AND password='"+upassword+"'");   
	    ResultSet rs=prst.executeQuery();
	         
	       while(rs.next()) 
	     {
	  	   
	  	    username = rs.getString(1);
	  	    emp_id= rs.getString(2);
	  	    role_id = rs.getString(3); 
	  	 
	  	    flag=1;
	     }
	     if(flag==1)
	     {   session = request.getSession(true);
	    	 session.setAttribute("uname",username);
	  	   session.setAttribute("emp_id",emp_id);
	  	   session.setAttribute("role_id",role_id);
	  	 response.sendRedirect("/Genius/form/general/userLogged.jsp"); //logged-in page
	     }
	     else if(uname.equals(null) || upassword.equals(null))
	     {response.sendRedirect("/Genius/form/general/Login.jsp");}
	     else
	     {// session = request.getSession(false);
	    	// session.setAttribute("uname",username);
		  	//   session.setAttribute("emp_id",emp_id);
		  	 //  session.setAttribute("role_id",role_id);
	     response.sendRedirect("/Genius/form/general/invalidLogin.jsp");
	     }
	    	     
	} 
			
			
	catch (Throwable theException) 	    
	{
	     System.out.println(theException); 
	}
       }


	}